﻿#include "GameScene.h"

USING_NS_CC;

const int MIDDLESCREEN = 1300;
const Point DIFFERENCESHADOW = Point(40, 20);

Scene* GameScene::createScene()
{
	auto scene = Scene::create();

	auto layer = GameScene::create();

	scene->addChild(layer);

	return scene;
}
bool GameScene::init()
{
	if (!Layer::init())
	{
		return false;
	}
	//srand(GetTickCount());
	srand(time(NULL));
	

	EventListenerTouchAllAtOnce* _touchListener = EventListenerTouchAllAtOnce::create();
		
	_touchListener->onTouchesBegan = CC_CALLBACK_2(GameScene::onTouchesBegan, this);
	_touchListener->onTouchesMoved = CC_CALLBACK_2(GameScene::onTouchesMoved, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_touchListener,1);


	//UserDefault::getInstance()->setFloatForKey("currentLevel", _currentLevel);
	_currentStg = UserDefault::getInstance()->getFloatForKey("currentStg", 1);

	player = Player::createPlayer(540, 500);
	this->addChild(player, kTag_player);


	
	_shadow = Sprite::create("shadow.png");
	_shadow->setAnchorPoint(Point(0.5, 0));
	_shadow->setPosition(Point(player->getPositionX() + DIFFERENCESHADOW.x, player->getPositionY() - DIFFERENCESHADOW.y));
	this->addChild(_shadow, kTag_shadow);

	player->exp = UserDefault::getInstance()->getFloatForKey("exp", player->exp);
	_expLabel = Label::createWithSystemFont("", "Arial", 55);
	_expLabel->setPosition(Point(900, 1600));
	_expLabel->setColor(Color3B(0, 0, 0));
	this->addChild(_expLabel, kTag_label);
	
	sprintf(tmp, "%.0f", player->exp);
	_expLabel->setString(tmp);
	


	_StgLabel = Label::createWithSystemFont("레벨1", "Arial", 50);
	_StgLabel->setPosition(Point(150, 1600));
	_StgLabel->setColor(Color3B(0, 0, 0));
	this->addChild(_StgLabel,kTag_label);

	_lvUpConsole = Sprite::create("consolebox.png");
	_lvUpConsole->setPosition(Point(540, 600));
	_lvUpConsole->setVisible(false);
	this->addChild(_lvUpConsole, kTag_UI);

	_lvUpLabel = Label::createWithSystemFont("", "Arial", 55);
	_lvUpLabel->setPosition(Point(540, 600));
	_lvUpLabel->setColor(Color3B(0, 0, 0 ));
	_lvUpLabel->setVisible(false);
	this->addChild(_lvUpLabel, kTag_label);

	gameMain();
	updateExpBar();
	playerMove();
	scheduleUpdate();
	
	//this->schedule(schedule_selector(GameScene::createMamma), 10.0f);
	float _rand = CCRANDOM_0_1() * 4 + 6;
	DelayTime* _delay = DelayTime::create(_rand);
	Sequence* _seq = Sequence::create(_delay, CallFunc::create(CC_CALLBACK_0(GameScene::createMamma, this)), NULL);
	this->runAction(_seq);
	
	return true;
}
void GameScene::createMamma()
{
	
		int mammaW = CCRANDOM_0_1() * 830 + 120;
		int mammaH = CCRANDOM_0_1() * 750 + 250;


		mamma1 = Mamma::createMamma(mammaW, mammaH);
		
		this->addChild(mamma1, kTag_mamma);
		mamma1List.pushBack(mamma1);
		float _scaleForPosY2 = ((abs(mamma1->getPositionY() - MIDDLESCREEN)) / MIDDLESCREEN + 1 );
		mamma1->setScale(_scaleForPosY2);
		float _rand = CCRANDOM_0_1()*2+1;
		//float _rand = CCRANDOM_0_1() * 4 + 6;
		DelayTime* _delay = DelayTime::create(_rand);
		Sequence* _seq = Sequence::create(_delay, CallFunc::create(CC_CALLBACK_0(GameScene::createMamma, this)), NULL);
		this->runAction(_seq);
}

void GameScene::update(float dt) { 

	float _scaleForPosY = ((abs(player->getPositionY() - MIDDLESCREEN)) / MIDDLESCREEN + 1);
	
	player->setScale(_scaleForPosY);
	
	

} 

void GameScene::gameMain()
{
	Sprite* _gameBg = Sprite::create("bg.jpg");
	_gameBg->setAnchorPoint(Point::ZERO);
	_gameBg->setPosition(Point::ZERO); 
	this->addChild(_gameBg,kTag_background);



//////////////////////////////////////// UI �ϴ� ��ư ���� �κ� /////////////////////////////////////////////
	Sprite* _outDoorSprite = Sprite::create("UI/outdoor_button.png");
	MenuItemSprite* _outDoorButton = MenuItemSprite::create(_outDoorSprite, _outDoorSprite, CC_CALLBACK_1(GameScene::SelectMenu, this, "outdoor"));
	_outDoorButton->setAnchorPoint(Point::ZERO);
	_outDoorButton->setPosition(Point(150, 70));

	Sprite* _historySprite = Sprite::create("UI/history_button.png");
	MenuItemSprite* _historyButton = MenuItemSprite::create(_historySprite, _historySprite, CC_CALLBACK_1(GameScene::SelectMenu, this, "history"));
	_historyButton->setAnchorPoint(Point::ZERO);
	_historyButton->setPosition(Point(500, 70));

	Sprite* _storeSprite = Sprite::create("UI/store_button.png");
	MenuItemSprite* _storeButton = MenuItemSprite::create(_storeSprite, _storeSprite, CC_CALLBACK_1(GameScene::SelectMenu, this, "store"));
	_storeButton->setAnchorPoint(Point::ZERO);
	_storeButton->setPosition(Point(840, 70));

	Menu* menu = Menu::create(_outDoorButton, _historyButton, _storeButton, NULL);
	menu->setAnchorPoint(Point::ZERO);
	menu->setPosition(Point::ZERO);
	addChild(menu, kTag_UI);

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	Sprite* _expBox = Sprite::create("expui.png");
	
	_expBox->setPosition(Point(500, 1600));
	this->addChild(_expBox, kTag_UI);
	_expBar = Sprite::create("expbar.png");
	_expBar->setAnchorPoint(Point(0, 0.5));
	_expBar->setPosition(Point(170, 1550));
	_expBar->setScaleX(0);
	this->addChild(_expBar, kTag_UI);



}
void GameScene::playerMove()
{
	
	//200~1700 / 100~900
	int _randX = CCRANDOM_0_1() * 500 + 300;
	int _randY = CCRANDOM_0_1() * 600 + 400;
	//CCLOG("player position2 x : %d", _randX);
	//CCLOG("player position2 y : %d", _randY);
	JumpTo* _jump = JumpTo::create(0.5, Point(_randX, _randY), 150, 1);
	EaseInOut* _ease = EaseInOut::create(_jump, 0.3);
	DelayTime* _delay = DelayTime::create(3.0f);
	Sequence* _seq = Sequence::create(_ease, _delay,CCCallFunc::create(CC_CALLBACK_0(GameScene::playerMove, this)), NULL);
	
	MoveTo* _move = MoveTo::create(0.5, Point(_randX + DIFFERENCESHADOW.x, _randY - DIFFERENCESHADOW.y));
	EaseInOut* _ease2 = EaseInOut::create(_move, 0.3);
	Sequence* _seq2 = Sequence::create(_ease2, _delay, NULL);


	float _MaxScale = ((float)((abs(_randY - MIDDLESCREEN)) / (float)MIDDLESCREEN) + 1);
	float _MinScale = _MaxScale * 0.7;
	//CCLOG("MAXSCALE : %f", _MaxScale); 
	//CCLOG("SHADOWSCALE : %f", _shadow->getScale());
	
	FadeTo* _fade = FadeTo::create(0.25,200);
	FadeTo* _fade2 = FadeTo::create(0.25, 255);

	ScaleTo* _shadowScale = ScaleTo::create(0.25, _MinScale);
	ScaleTo* _shadowScale2 = ScaleTo::create(0.25, _MaxScale);
	EaseIn* _shadowEase1 = EaseIn::create(_shadowScale, 0.5);
	EaseOut* _shadowEase2 = EaseOut::create(_shadowScale2, 0.5);

	Sequence* _shadowSeq = Sequence::create(_shadowEase1, _shadowEase2, _delay, NULL);
	Sequence* _shadowSeq2 = Sequence::create(_fade, _fade2, _delay, NULL);
	Spawn* _spawn = Spawn::create(_seq2, _shadowSeq,_shadowSeq2, NULL);
	
	player->runAction(_seq);
	//player->isMoving = true;
	_shadow->runAction(_spawn);
	
	
	
}
void GameScene::particle()
{
	ParticleSystemQuad* _particle = ParticleSystemQuad::create("particle_texture.plist");
	ParticleSystemQuad* _particle2 = ParticleSystemQuad::create("particle_texture.plist");
	_particle->setPosition(Point(300, 850));
	_particle2->setPosition(Point(850, 500));
	this->addChild(_particle, kTag_label);
	this->addChild(_particle2, kTag_label);

}
void GameScene::removeMamma(Mamma* touchedMamma)
{
	this->removeChild(touchedMamma);
	
	player->exp += touchedMammaList.at(0)->exp;
	sprintf(tmp, "%.0f", player->exp);
	UserDefault::getInstance()->setFloatForKey("exp", player->exp);
	UserDefault::getInstance()->flush();
	_expLabel->setString(tmp);

	touchedMammaList.eraseObject(touchedMamma);
	mamma1List.eraseObject(touchedMamma);
	player->isMoving = false;
	if (touchedMammaList.size() != 0)
	{
		
		goToMamma();
	}
	/*else
	{
		playerMove();
	}*/
	updateExpBar();
}
void GameScene::goToMamma()
{
	if (player->isMoving == false && touchedMammaList.size() != 0)
	{
		player->isMoving = true;
		JumpTo* _jump = JumpTo::create(0.5f, Point(touchedMammaList.at(0)->getPositionX(), touchedMammaList.at(0)->getPositionY()), 180, 1);
		EaseInOut* _ease = EaseInOut::create(_jump, 0.3);
		DelayTime* _delay = DelayTime::create(1.5f);
		Sequence* _seq = Sequence::create(_ease,
			CCCallFunc::create(CC_CALLBACK_0(GameScene::removeMamma, this, touchedMammaList.at(0))),
			//CCCallFunc::create(CC_CALLBACK_0(GameScene::goToMamma, this)), 
			_delay,
			CCCallFunc::create(CC_CALLBACK_0(GameScene::playerMove, this)), 
			NULL);
		player->stopAllActions();
		player->runAction(_seq);

		_shadow->stopAllActions();
		MoveTo* _move = MoveTo::create(0.5, Point(touchedMammaList.at(0)->getPositionX() + DIFFERENCESHADOW.x, touchedMammaList.at(0)->getPositionY() - DIFFERENCESHADOW.y));
		EaseInOut* _ease2 = EaseInOut::create(_move, 0.3);
		Sequence* _seq2 = Sequence::create(_ease2, _delay, NULL);

		float _MaxScale = ((float)((abs(player->getPositionY() - MIDDLESCREEN)) / (float)MIDDLESCREEN) + 1);
		float _MinScale = _MaxScale * 0.7;
		//CCLOG("MAXSCALE : %f", _MaxScale); 
		//CCLOG("SHADOWSCALE : %f", _shadow->getScale());

		FadeTo* _fade = FadeTo::create(0.25, 200);
		FadeTo* _fade2 = FadeTo::create(0.25, 255);

		ScaleTo* _shadowScale = ScaleTo::create(0.25, _MinScale);
		ScaleTo* _shadowScale2 = ScaleTo::create(0.25, _MaxScale);
		EaseIn* _shadowEase1 = EaseIn::create(_shadowScale, 0.5);
		EaseOut* _shadowEase2 = EaseOut::create(_shadowScale2, 0.5);

		Sequence* _shadowSeq = Sequence::create(_shadowEase1, _shadowEase2, _delay, NULL);
		Sequence* _shadowSeq2 = Sequence::create(_fade, _fade2, _delay, NULL);
		Spawn* _spawn = Spawn::create(_seq2, _shadowSeq, _shadowSeq2, NULL);
		_shadow->runAction(_spawn);
	}
	
	

}


void GameScene::onTouchesBegan(const std::vector<Touch*>&touches, Event* unused_event)
{
	if (_lvUpConsole&& _lvUpLabel)
	{

		_lvUpConsole->setVisible(false);
		_lvUpLabel->setVisible(false);
	}
	Point location = touches.at(0)->getLocation();

	for (int i = 0; i < mamma1List.size(); i++)
	{
		if (mamma1List.at(i)->getBoundingBox().containsPoint(location) && mamma1List.at(i)->isTouched == false)
		{
			touchedMammaList.pushBack(mamma1List.at(i));
			mamma1List.at(i)->isTouched = true;
			//player->stopAllActions();
			//player->isMoving = false;
			goToMamma();
			return;
		}
		
	}
	

	//setSwallowsTouches(true);
	//CCLOG("eeeeeeeeeeeeeexp : %.0f",player->exp);	
	//updateExpBar();

}
void GameScene::onTouchesMoved(const std::vector<Touch*>&touches, Event* unused_event)
{
	Point _location = touches.at(0)->getLocation();
	for (int i = 0; i < mamma1List.size(); i++)
	{
		if (mamma1List.at(i)->getBoundingBox().containsPoint(_location) && mamma1List.at(i)->isTouched == false)
		{
			touchedMammaList.pushBack(mamma1List.at(i));
			mamma1List.at(i)->isTouched = true;
			//player->stopAllActions();
			//player->isMoving = false;
			goToMamma();
		}
	}
	//updateExpBar();
}
void GameScene::updateExpBar()
{
	switch (_currentStg)
	{
		case 1:_expPercent = player->exp / (float)Stg_1;
			_currentLevel = Stg_1;
		
			break;
	
		case 2:_expPercent = (player->exp -  Stg_1) / (float)Stg_2; 
			_currentLevel = Stg_2;
			_StgLabel->setString("레벨2");
			_lvUpLabel->setString("레벨 2(이)가 되었습니다");
			player->setTexture("player2.png");
			UserDefault::getInstance()->setFloatForKey("currentStg", 2);
			UserDefault::getInstance()->flush();
			break;
	
		case 3: _expPercent =( player->exp -  (Stg_1 + Stg_2)) /(float)Stg_3;
			_currentLevel = Stg_3;
			_StgLabel->setString("레벨3");
			_lvUpLabel->setString("레벨 3(이)가 되었습니다");
			player->setTexture("player3.png");
			UserDefault::getInstance()->setFloatForKey("currentStg", 3);
			UserDefault::getInstance()->flush();
			break;
	
		case 4:_expPercent = (player->exp - (Stg_1 + Stg_2 + Stg_3)) / (float)Stg_4;
			_currentLevel = Stg_4;
			_StgLabel->setString("레벨4");
			_lvUpLabel->setString("레벨 4(이)가 되었습니다");
			player->setTexture("player4.png");
			UserDefault::getInstance()->setFloatForKey("currentStg", 4);
			UserDefault::getInstance()->flush();
			break;
	
		case 5:_expPercent = (player->exp - (Stg_1 + Stg_2 + Stg_3 + Stg_4)) / (float)Stg_5;
			_currentLevel = Stg_5;
			_StgLabel->setString("레벨5");
			_lvUpLabel->setString("레벨 5(이)가 되었습니다");
			player->setTexture("player5.png");
			UserDefault::getInstance()->setFloatForKey("currentStg", 5);
			UserDefault::getInstance()->flush();
			break;
	
		case 6:_expPercent = (player->exp - (Stg_1 + Stg_2 + Stg_3 + Stg_4 + Stg_5)) / (float)Stg_6;
			_currentLevel = Stg_6;
			_StgLabel->setString("레벨6");
			_lvUpLabel->setString("레벨 6(이)가 되었습니다");
			UserDefault::getInstance()->setFloatForKey("currentStg", 6);
			UserDefault::getInstance()->flush();
			break;
		default: 
			_expPercent = 1;
			break;
	}
	
	if (_expPercent > 1 && _currentStg == 6)
	{
		_currentStg++;
		updateExpBar();

	}
	else if (_expPercent > 1) 
	{
		_currentStg++;
		particle();
		_lvUpConsole->setVisible(true);
		_lvUpLabel->setVisible(true);
		updateExpBar();
	}
	 
	

	
	_expBar->setScaleX(_expPercent);
	/*CCLOG("expPercent : %f", _expPercent);
	CCLOG("_currentStg : %i", _currentStg);*/

}

GameScene::GameScene()
{
}


GameScene::~GameScene()
{
	//CC_SAFE_RELEASE(_lvUpLabel);
}



void GameScene::SelectMenu(Ref* tmp, const char* str) {

	CCLOG("STR : %s", str);
	if (str == "outdoor") {
		Director::getInstance()->pushScene(OutDoor::createScene());
		CCLOG("%d", 1);
	}
	else if (str == "history"){
		Director::getInstance()->pushScene(Album::createScene()); 
		CCLOG("%d", 2);
	}
	else if (str == "store"){
		Director::getInstance()->pushScene(Store::createScene()); 
		CCLOG("%d", 3);
	}
}

